package com.samebuilding.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.springframework.transaction.annotation.Transactional;

import com.samebuilding.dao.UserDAO;
import com.samebuilding.domain.User;
import com.samebuilding.service.UserService;

public class UserServiceImp implements UserService{

	private UserDAO userDAO;

	@Transactional
	public void save(User u) {

		userDAO.save(u);
	}

	public void update(User u) {

		userDAO.update(u);
	}
	public List findAll(){
		return userDAO.findAll();
	}
	
	public User findByEmail(String email){
		return userDAO.findByEmail(email);
	}
	
	
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public List findVIP() {
		
		return userDAO.findVIP();
	}

	public User findByUserNo(int userNo) {
		return userDAO.findByUserNo(userNo);
	}

	public List findSameRegionCode(int code) {
		return userDAO.findSameRegionCode(code);
	}

	public List findSameBuilding(int code) {
		return userDAO.findSameBuilding(code);
	}

	public boolean isVIP(int userNo) {
		return userDAO.isVIP(userNo);
	}

	public List findByCon(User u, int age) {
		return userDAO.findByCon(u, age);
	}
	public List getLoginNow(int cityCode) {
		WebContext ctx = WebContextFactory.get();
		HttpServletRequest req = ctx.getHttpServletRequest();
		HttpSession session = req.getSession();
		ServletContext application = session.getServletContext();
		List<User> loginNow;
		if(session.getAttribute("loginNow")==null){
			loginNow = new ArrayList<User>();
		}else{
			loginNow = (List<User>)session.getAttribute("loginNow");
		}
		List<User> onlineUser = (List<User>)application.getAttribute("onlineUser");
		Iterator it = onlineUser.iterator();
		if(loginNow.size()<10){
			while(loginNow.size()<10&&it.hasNext()){
				User u = (User)it.next();
				if(u.getCityCode()==cityCode&&!loginNow.contains(u)){
					loginNow.add(u);
				}
			}
			session.setAttribute("loginNow", loginNow);
			return loginNow;
		}else{
			List<User> tenSecond = new ArrayList<User>();
			Date d = new Date();
			d.setSeconds(d.getSeconds()-10);
			while(it.hasNext()){
				User u = (User)it.next();
				if(u.getUserData().getLoginDate().after(d)){
					tenSecond.add(u);
				}
			}
			Collections.sort(tenSecond);
			int i = 0;
			while(i<tenSecond.size()){
				if(((User)tenSecond.get(i)).getCityCode()==cityCode&&!loginNow.contains(tenSecond.get(i))){
					loginNow.add((User)tenSecond.get(i));
					loginNow.remove(0);
				}
				i++;
			}
			session.setAttribute("loginNow", loginNow);
			return loginNow;
		}
	}

	public boolean allowComment(int userNo) {
		return this.userDAO.isComment(userNo);
	}

	public boolean allowLeave(int userNo) {
		return this.userDAO.isLeave(userNo);
	}
	
	
	public List<User> findBySimpleCondition(User u, int age, int start, int limit) {
		
		return this.userDAO.findBySimpleCondition(u, age, start, limit);
	}

	public int findCountBySimpleCondition(User u, int age) {
		
		return this.userDAO.findCountBySimpleCondition(u, age);
	}

}
